package com.zhanglj.gis;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.geotools.data.FeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.io.WKTReader;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

/**
 *
 * @author asdf
 * @date 2021年3月2日 下午5:21:48
 */
public class GisUtils {

	public static FeatureIterator<SimpleFeature> itertor = null;
	public static int count = 0;

	public static List<String> mList = new ArrayList<>();

	static final GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);

	static String[] mGpsBuffer = null;

	public static FeatureIterator<SimpleFeature> readSHP() {
		ShapefileDataStore shpDataStore = null;
		long time1 = System.currentTimeMillis();
		try {
			// shp文件路径
			shpDataStore = new ShapefileDataStore(new File("E:\\res\\map\\CHN_city_map.shp").toURI().toURL());
			shpDataStore.setCharset(Charset.forName("GBK"));
			// 文件名称
			String typeName = shpDataStore.getTypeNames()[0];
			System.out.println(typeName);
			FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = null;
			featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) shpDataStore.getFeatureSource(typeName);
			FeatureCollection<SimpleFeatureType, SimpleFeature> result = featureSource.getFeatures();
			itertor = result.features();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(System.currentTimeMillis() - time1 + "读取shp文件时间");
		return itertor;
	}

	/**
	 * 根据个gps坐标获取对应城市
	 * @param poingX
	 * @param pointY
	 * @return
	 */
	public static String readInfo(String poingX, String pointY) {
		StringBuffer mBuffer = null;
		String mCityName = "";
		try {
			if (null == itertor) {
				itertor = readSHP();
			}
			mBuffer = new StringBuffer();
			// 迭代iterator
			while (itertor.hasNext()) {
				SimpleFeature feature = itertor.next();
				Collection<Property> p = feature.getProperties();
				Iterator<Property> it = p.iterator();
				while (it.hasNext()) {
					String value = null;
					Property pro = it.next();
					value = pro.getValue().toString();
					mBuffer.append(value + "\r\n");
				}
				count++;
				mList.add(mBuffer.toString());
				mBuffer.delete(0, mBuffer.length());
			}
			//itertor.close();
			String data = null;
			String[] arr = null;
			// 如果缓存mGpsBuffer为空，则从新到所有集合中循环。
			if (null != mGpsBuffer) {
				WKTReader reader = new WKTReader(geometryFactory);
				String point1 = "POINT(" + poingX + " " + pointY + ")";
				System.out.println(point1);
				Point point = (Point) reader.read(point1);
				System.out.println(point);
				MultiPolygon multiPolygon = (MultiPolygon) reader.read(mGpsBuffer[0]);
				// 传过来的gps经纬度坐标，为一个点
				Geometry geometry1 = geometryFactory.createGeometry(point);
				// 区域内所有点围城的一个多边形
				Geometry geometry2 = geometryFactory.createGeometry(multiPolygon);
				// 如果该点包含在多边形里面则解析出城市名
				if (geometry2.contains(geometry1)) {
					mCityName = mGpsBuffer[1];
					System.out.println("cityname1 = " + mGpsBuffer[1]);
					return mCityName;
				}
			}
			//循环所有点集合，判断点是否在其中的多边形里面。
			for (int i = 0; i < mList.size(); i++) {
				data = mList.get(i);
				arr = data.split("\r\n");
				try {
					mGpsBuffer = new String[arr.length];
					WKTReader reader = new WKTReader(geometryFactory);
					String point1 = "POINT(" + poingX + " " + pointY + ")";
					Point point = (Point) reader.read(point1);
					MultiPolygon multiPolygon = (MultiPolygon) reader.read(arr[0]);
					Geometry geometry1 = geometryFactory.createGeometry(point);
					Geometry geometry2 = geometryFactory.createGeometry(multiPolygon);
					if (geometry2.contains(geometry1)) {
						mCityName = arr[4];
						//将多边形的信息存入缓存中。
						mGpsBuffer = arr;
						System.out.println("cityname = " + arr[4]);
						break;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return mCityName;
	}

	public static void main(String[] args) {
		long starTime = System.currentTimeMillis();
		GisUtils.readInfo("99.72", "26.04");
		long endTime = System.currentTimeMillis();
		System.out.println((endTime - starTime));
	}
}
